package org.ym.file.domain.model.node.common.upload;

import org.ym.tools.FileTool;
import org.ym.tools.ZipTool;

import java.nio.file.Path;
import java.util.List;

/**
 * 加密下载
 * @author ym
 */
public class EncryptDownload implements DownloadInterface{
    private static EncryptDownload instance;

    /**
     * 获取实例
     * @return
     */
    public static EncryptDownload getInstance(){
        if(instance == null){
            synchronized (EncryptDownload.class){
                if(instance == null){
                    instance = new EncryptDownload();
                }
            }
        }
        return instance;
    }
    @Override
    public FileDownloadResult download(FileDownloadCommand fileDownloadCommand) {
        List<Path> unZipFileList;
        try {
            unZipFileList = ZipTool.unZip(fileDownloadCommand.getFilePath(), fileDownloadCommand.getWorkFolderPath(), fileDownloadCommand.getPassword());
            if (!unZipFileList.isEmpty()) {
                return FileDownloadResult.builder().filePath(unZipFileList.get(0)).build();
            } else {
                return FileDownloadResult.builder().build();
            }
        } catch (Exception e) {
            FileTool.deleteFile(fileDownloadCommand.getWorkFolderPath());
            throw e;
        }
    }
}
